package com.ste.server.command;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ste.domain.Order;
import com.ste.server.Server;

public class TradeWithMarketPrice extends Trade {
	public static final String regexp = "35=D\\|11=(.*)\\|55=(.*)\\|54=(.*)\\|38=(.*)\\|40=(.*)";

	public TradeWithMarketPrice(Server server, Command command) {
		super(server, command);

	}

	@Override
	protected Order parseMessage(String message) {
		String orderId = null;
		Integer lots = null;
		OrderType type = null;
		Matcher matcher = Pattern.compile(regexp).matcher(command.getMessage());
		if (matcher.matches()) {
			orderId = matcher.group(1);
			stokeName = matcher.group(2);
			type = "1".equals(matcher.group(3)) ? OrderType.BUY : OrderType.SELL;
			try {
				lots = Integer.valueOf(matcher.group(4));
				if (lots <= 0 ){
					throw new NumberFormatException(orderId);
				}
			} catch (NumberFormatException e) {
				throw new NumberFormatException(orderId);
			}
		}
		return new Order(command.getClient().getName(), orderId, type, null, lots);
	}

}
